package layers.ants;

import layers.physical.Crossroad;
import layers.physical.Road;


public class ExplorationAntStateFollowingRoadsigns extends ExplorationAntState {

	public ExplorationAntStateFollowingRoadsigns(ExplorationAnt ant) {
		super(ant);
	}

	@Override
	protected void processTick() {
		int truckId = this.getAnt().getId();
		Crossroad crossroad = this.getAnt().getCrossroad();
		this.getAnt().decreaseRemainingHops();
		this.getAnt().increaseTraveledDistance(this.getAnt().getLengthOfLastRoad());
		if(this.getAnt().getRemainingHops() > 0){
			if(crossroad.hasPackage()){
				this.foundPackage(crossroad);
			}else{
				Road road = this.getAnt().getCrossroad().getRoadWithMostPheromones();
				if(road != null)
					followRoadSigns(road, truckId, crossroad);
				else
					this.getAnt().die();
			}
		}else{
			this.getAnt().die();
		}
	}

	private void foundPackage(Crossroad crossroad) {
		this.getAnt().setState(new ExplorationAntStateFoundPackage(getAnt()));
		this.clearAnt();
	}
}
